********************************************************************************
******"Macroprudential Regulation, Quantitative Easing, and Bank Lending"*******
********************************************************************************

********************************************************************************
********************************************************************************
**********************************2019******************************************
********************************************************************************
********************************************************************************
global ec4 "dT1e14 dT2e14 dT3e14 dT4e14 dT5e14 dT7e14 dT8e14 dT9e14 dT10e14 dT11e14 dT12e14"
global ed4top "dT1dtop66e14 dT2dtop66e14 dT3dtop66e14 dT4dtop66e14 dT5dtop66e14 dT7dtop66e14 dT8dtop66e14 dT9dtop66e14 dT10dtop66e14 dT11dtop66e14 dT12dtop66e14"
global ed4mid "dT1dmid6633e14 dT2dmid6633e14 dT3dmid6633e14 dT4dmid6633e14 dT5dmid6633e14 dT7dmid6633e14 dT8dmid6633e14 dT9dmid6633e14 dT10dmid6633e14 dT11dmid6633e14 dT12dmid6633e14"
global controls "lncrefin dT*exlnassets dT*excashas"
global regcommands ", pool(5) compact absorb(bankid time#isin) vce(cluster bankid isin)"

clear *
**# Bookmark (input)
use $inputdirectory/INB_portfolio2019, replace

gen die=0
replace die=1 if inlist(staemitit,8,9,101,94,257,67,28,29,40,86,259,92,258,105,50,55,260,276,477,479,480,574,576,577,580,587,588,591,595,799,807,812,814,816,853,910,911,913,916,921,929,940,945,950,959,974,32)&inlist(setemi,100,704,770,173,706,708,121,120)&inlist(stanziabilita,23,24)&(monthtom>=15)&(monthtom<=375)&(monthtom!=.)&time==tm(2019m8)

order bankid time cod_isin book portfolio, first
keep bankid time cod_isin book portfolio die
rename cod_isin COD_ISIN

egen dielong=max(die), by(COD_ISIN)
keep if dielong==0

keep if portfolio==3|portfolio==2

drop portfolio
collapse (sum) book, by(bankid time COD_ISIN)

**# Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_preann2019m8, keepus(exbdisbur)
keep if _merge==3|_merge==2
drop _merge
replace time=tm(2019m8) if time==.
drop exbdisbur

fillin bankid COD_ISIN time
replace book=0 if book==.
egen isin=group(COD_ISIN)
drop if isin==.
egen id=group(bankid isin)
xtset id time

**# Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_preann2019m8, keepus(e14 dtop66e14 dmid6633e14 exlnassets excashas)
keep if _merge==3
drop _merge

**# Bookmark (input)
merge m:1 bankid time using $inputdirectory/INB_assets2019, keepus(assets)
keep if _merge!=2
drop _merge

**# Bookmark (input)
merge m:1 bankid time using $inputdirectory/INB_refinm2019, keepus(lncrefin)
keep if _merge!=2
drop _merge
recode lncrefin (.=0)

**# Bookmark (input)
merge m:1 time COD_ISIN using $inputdirectory/INB_isinsprices2019
keep if _merge!=2
drop _merge
replace price=. if price==0
replace priceavg=. if priceavg==0

gen quantity=book/price
xtset id time

gen qsold=quantity-L.quantity
gen sales=1000000*(qsold)*priceavg/L.assets

keep if time>=tm(2019m3)&time<=tm(2020m2)
egen xxx=mean(book), by(bankid isin)
sum xxx if xxx!=0, d
drop if abs(xxx)<=10000&xxx!=0
drop xxx
winsor2 sales if sales!=0&sales!=., suffix(_w) cut(1 99)
recode sales_w (.=0)

run RO_tdummies 2019 12
run RO_tdintera 12 "e14 dtop66e14 dmid6633e14 exlnassets excashas"

**# Bookmark (output - columns 1 and 2)
cd $outputdirectory
est clear
reghdfe sales_w $ec4 $controls $regcommands
est save TABIIIC_C1234
reghdfe sales_w $ed4top $ed4mid $controls $regcommands
est save TABIIIC_C1234, append
cd ..


********************************************************************************
********************************************************************************
**********************************2015******************************************
********************************************************************************
********************************************************************************
global ec3 "dT1e13 dT2e13 dT3e13 dT4e13 dT5e13 dT7e13 dT8e13 dT9e13 dT10e13 dT11e13 dT12e13"
global ed3top "dT1dtop85e13 dT2dtop85e13 dT3dtop85e13 dT4dtop85e13 dT5dtop85e13 dT7dtop85e13 dT8dtop85e13 dT9dtop85e13 dT10dtop85e13 dT11dtop85e13 dT12dtop85e13"
global controls "lncrefin dT*exlnassets dT*excashas"
global regcommands ", pool(5) compact absorb(bankid time#isin) vce(cluster bankid isin)"

clear *
**# Bookmark (input)
use $inputdirectory/INB_portfolio2015, replace

gen die=0
replace die=1 if inlist(staemitit,8,9,101,94,257,67,28,29,40,86,259,92,258,105,50,55,260,276,477,479,480,574,576,577,580,587,588,591,595,799,807,812,814,816,853,910,911,913,916,921,929,940,945,950,959,974)&inlist(setemi,100,704,770)&inlist(stanziabilita,23,24)&(monthtom>=27)&(monthtom<=375)&(monthtom!=.)&time==tm(2014m12)

order bankid time cod_isin book portfolio, first
keep bankid time cod_isin book portfolio die
rename cod_isin COD_ISIN

egen dielong=max(die), by(COD_ISIN)
keep if dielong==0

keep if portfolio==3|portfolio==2

drop portfolio
collapse (sum) book, by(bankid time COD_ISIN)

**# Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_preann2014m12, keepus(exassets)
keep if _merge==3|_merge==2
drop _merge
replace time=tm(2014m12) if time==.
drop exassets

fillin bankid COD_ISIN time
replace book=0 if book==.
egen isin=group(COD_ISIN)
drop if isin==.
egen id=group(bankid isin)
xtset id time

**# Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_preann2014m12, keepus(e13 dtop85e13 exlnassets excashas)
keep if _merge==3
drop _merge

**# Bookmark (input)
merge m:1 bankid time using $inputdirectory/INB_assets2015, keepus(assets)
keep if _merge!=2
drop _merge

**# Bookmark (input)
merge m:1 bankid time using $inputdirectory/INB_refinm2015, keepus(lncrefin)
keep if _merge!=2
drop _merge
recode lncrefin (.=0)

**# Bookmark (input)
merge m:1 time COD_ISIN using $inputdirectory/INB_isinsprices2015
keep if _merge!=2
drop _merge
replace price=. if price==0
replace priceavg=. if priceavg==0

gen quantity=book/price
xtset id time

gen qsold=quantity-L.quantity
gen sales=1000000*(qsold)*priceavg/L.assets

keep if time>=tm(2014m7)&time<=tm(2015m6)
egen xxx=mean(book), by(bankid isin)
sum xxx if xxx!=0, d
drop if abs(xxx)<=10000&xxx!=0
drop xxx
winsor2 sales if sales!=0&sales!=., suffix(_w) cut(1 99)
recode sales_w (.=0)

run RO_tdummies 2015 12
run RO_tdintera 12 "e13 dtop85e13 exlnassets excashas"

**# Bookmark (output - columns 3 and 4)
cd $outputdirectory
est clear
reghdfe sales_w $ec3 $controls $regcommands
est save TABIIIC_C1234, append
reghdfe sales_w $ed3top $controls $regcommands
est save TABIIIC_C1234, append
cd ..


